package com.realw.argus.web.util;

import java.io.StringWriter;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.tools.ToolManager;

public class VelocityUtilDemo {

	public static void main(String[] args) {
		VelocityEngine ve = new VelocityEngine();
		ve.init("D:\\SVN\\Google\\Code\\ArgusWeb\\trunk\\src\\main\\resources\\velocity\\velocity.properties");

		StringResourceLoader.getRepository().putStringResource("test1",
				"[$error]");
		StringResourceLoader.getRepository().putStringResource("test2",
				"$error");

		Context ctx = getContext2();
		System.out.println(ctx);
		ctx.put("msg", "Hello World!");
		ctx.put("error", "ERROR001");

		StringWriter writer = new StringWriter();
		ve.evaluate(ctx, writer, "MyVelocityTest",
				"$!msg $stringUtils.isBlank('') #parse('test1') #parse('test2')");

		System.out.println(writer.getBuffer().toString());
	}

	public static Context getContext1() {
		Context ctx = new VelocityContext();
		return ctx;
	}

	public static Context getContext2() {
		ToolManager manager = new ToolManager();
		manager.configure("velocity\\toolboxV2.xml");
		return manager.createContext();
	}

}
